package com.lwh.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwh.yygh.common.result.Result;
import com.lwh.yygh.common.utlis.MD5;
import com.lwh.yygh.hosp.service.HospitalService;
import com.lwh.yygh.model.hosp.HospitalSet;
import com.lwh.yygh.vo.hosp.HospitalSetQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author lwh
 * @date 2021/5/16 15:25
 */
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
@CrossOrigin
public class HospitalSetController {

    @Autowired
    private HospitalService hospitalService;

    //查询医院设置表里面所有信息
    @GetMapping("findAll")
    public Result findAll(){
        List<HospitalSet> list = hospitalService.list();
        Result<List<HospitalSet>> result = Result.ok(list);
        return result;
    }


    //根据id删除
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        boolean flag = hospitalService.removeById(id);
        if (flag){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }


    //条件分页查询
    @PostMapping("findPage/{current}/{limit}")
    public Result findPageHosp(@PathVariable("current") Long current,
                               @PathVariable("limit") Long limit,
                               @RequestBody(required = false) HospitalSetQueryVo vo){
        //为什么要传入一个这样的参数，因为page的源码里面返回的就是一个List<T>
        Page<HospitalSet> page=new Page<>(current,limit);
        QueryWrapper<HospitalSet> query=new QueryWrapper<>();
        //医院名称
        String hoscode = vo.getHoscode();
        //医院编号
        String hosname = vo.getHosname();

        if (!StringUtils.isEmpty(hosname)){
            query.like("hosname",hosname);
        }
        if (!StringUtils.isEmpty(hoscode)){
            query.like("hoscode",hoscode);
        }


        IPage<HospitalSet> hospPage = hospitalService.page(page, query);

        return Result.ok(hospPage);

    }

    //添加医院设置
    @PostMapping("save")
    public Result saveHospital(@RequestBody HospitalSet hospitalSet){
        //设置状态1可用，0不可用
        hospitalSet.setStatus(1);
        //设置每个医院和平台对接的密钥
        Random random=new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
        boolean flag = hospitalService.save(hospitalSet);
        if (false){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //根据id获取医院设置
    @GetMapping("getHospSet/{id}")
    public Result getHospById(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalService.getById(id);
        return Result.ok(hospitalSet);
    }

    //修改医院设置
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet){

        boolean flag = hospitalService.updateById(hospitalSet);

        if (false){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //批量删除医院
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        hospitalService.removeByIds(ids);
        return Result.ok();

    }


    //医院设置锁定和解锁（就是对表中的status状态就行修改）
    @PutMapping("lockHospital/{id}/{status}")
    public Result lockHospital(@PathVariable("id") Long id,@PathVariable("status") Integer status){
        HospitalSet hospitalSet = hospitalService.getById(id);
        hospitalSet.setStatus(status);
        boolean save = hospitalService.updateById(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    //发送密钥
    @PutMapping("lockHospital/{id}")
    public Result sendKey(@PathVariable("id") Long id){
        HospitalSet hospitalSet = hospitalService.getById(id);
        String hoscode = hospitalSet.getHoscode();
        String hosname = hospitalSet.getHosname();
        //TODO  发送短信
        return Result.ok();

    }
}
